package com.jiang.daily.Q132;

import java.util.Arrays;

/**
 * @author Jiang
 * @version 1.0
 * @date 2025/03/02 18:58
 */
class Solution {

    public int minCut(String s) {
        int n = s.length();
        boolean[][] dp = new boolean[n][n];
        for (int i = 0; i < n; i++) {
            Arrays.fill(dp[i], true);
        }

        for (int i = n - 1; i >= 0; i--) {
            for (int j = i + 1; j < n; j++) {
                dp[i][j] = s.charAt(i) == s.charAt(j) && dp[i + 1][j - 1];
            }
        }

        int[] cnt = new int[n]; // cnt[i]表示 从 0 ~ i 子串中 构成回文子串的最少切割数
        Arrays.fill(cnt, Integer.MAX_VALUE);
        for (int i = 0; i < n; i++) {
            if (dp[0][i]) {
                cnt[i] = 0;
            } else {
                for (int j = 0; j < i; j++) {
                    if (dp[j + 1][i]) {
                        cnt[i] = Math.min(cnt[i], cnt[j] + 1);
                    }
                }
            }
        }
        return cnt[n - 1];
    }
}
